package com.sap.businessone.xapp;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

import org.junit.Test;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

public class MiscellaneousTest {

    private static final Logger log = LogManager.getLogger(MiscellaneousTest.class.getName());

    @Test
    public void simpleGetRequest() throws Exception {
        HttpGet httpget = new HttpGet("https://www.bing.com");
        log.info(httpget.getURI());

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String s = EntityUtils.toString(entity);

                log.info(s.length());
                log.info(s.substring(0, 100));
                log.info("truncated...");
            }
        } finally {
            response.close();
        }
    }

    @Test
    public void jsonResponse() throws Exception {
        HttpGet request = new HttpGet("http://msdn.microsoft.com/en-us/subscriptions/json/GetProductCategories?brand=MSDN&localeCode=en-us&_=1396658809209");
        log.info(request.getURI());

        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(request);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Gson gson = new GsonBuilder().create();
                ContentType contentType = ContentType.getOrDefault(entity);
                Charset charset = contentType.getCharset();
                Reader reader = new InputStreamReader(entity.getContent(), charset);
                Product[] products = gson.fromJson(reader, Product[].class);

                for (int i = 0; i < products.length; i++) {
                    Product p = products[i];
                    log.info(String.format("name=%s, brand=%d, id=%d", p.Name, p.Brand, p.ProductGroupId));
                }
            }
        } finally {
            response.close();
        }
    }

    @Test
    public void postRequest() throws Exception {
        HttpPost request = new HttpPost("http://msdn.microsoft.com/en-us/subscriptions/json/GetFileSearchResult");

        String ss = "{\"Languages\":\"en\",\"Architectures\":\"arm\",\"ProductFamilyIds\":\"\",\"FileExtensions\":\"\",\"MyProducts\":false,\"ProductFamilyId\":0,\"SearchTerm\":\"\",\"Brand\":\"MSDN\",\"PageIndex\":0,\"PageSize\":10,\"FileId\":58466}";

        StringEntity se = new StringEntity(ss, ContentType.APPLICATION_JSON);
        request.setEntity(se);

        // JsonObject obj;
        // com.google.gson.JsonElement ee;

        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(request);
        try {
            HttpEntity ee = response.getEntity();
            if (ee != null) {
                String s = EntityUtils.toString(ee);
                log.info(s.length());
                log.info(s);
            }
        } finally {
            response.close();
        }
    }

    private class Product {
        private Object ExtensionData;
        private int Brand;
        private String Name;
        private int ProductGroupId;

        public int getBrand() {
            return Brand;
        }

        public void setBrand(int brand) {
            Brand = brand;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }

        public int getProductGroupId() {
            return ProductGroupId;
        }

        public void setProductGroupId(int productGroupId) {
            ProductGroupId = productGroupId;
        }

        public Object getExtensionData() {
            return ExtensionData;
        }

        public void setExtensionData(Object extensionData) {
            ExtensionData = extensionData;
        }
    }
}
